package com.kmxd.ams.app.logs;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.logs.ILogSystemSvc;
import com.kmxd.ams.client.logs.dto.LogSystemAddDTO;
import com.kmxd.ams.client.logs.dto.LogSystemQueryDTO;
import com.kmxd.ams.client.logs.vo.LogSystemListVO;
import com.kmxd.ams.client.logs.vo.LogSystemVO;
import com.kmxd.ams.core.common.constant.Constants;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.logs.entity.LogSystem;
import com.kmxd.ams.infra.logs.mapper.LogSystemMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统操作日志 服务实现类
 *
 * @author ZuoLG
 * @since 2022-09-06
 */
@Service
@AllArgsConstructor
public class LogSystemSvcImpl extends ServiceImpl<LogSystemMapper, LogSystem>
    implements ILogSystemSvc {

  private LogSystemMapper logSystemMapper;

  @Override
  public Page<LogSystemListVO> selectPage(Page reqPage, LogSystemQueryDTO req) {
    LambdaQueryWrapper<LogSystem> queryWrapper =
        QueryGen.init(new QueryWrapper<LogSystem>(), req).lambda();
    queryWrapper.eq(
        LogSystem::getComId,
        ObjectUtil.isNotEmpty(SecurityUtil.getCropId())
            ? SecurityUtil.getCropId()
            : Constants.DEFAULT_CROP_ID);
    Page<LogSystem> page = logSystemMapper.selectPage(reqPage, queryWrapper);
    Page<LogSystemListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), LogSystemListVO.class));
    return voPage;
  }

  @Override
  public List<LogSystemListVO> selectList(LogSystemQueryDTO req) {
    LambdaQueryWrapper<LogSystem> queryWrapper =
        QueryGen.init(new QueryWrapper<LogSystem>(), req).lambda();
    queryWrapper.eq(
        LogSystem::getComId,
        ObjectUtil.isNotEmpty(SecurityUtil.getCropId())
            ? SecurityUtil.getCropId()
            : Constants.DEFAULT_CROP_ID);
    List<LogSystem> list = logSystemMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, LogSystemListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(LogSystemAddDTO req) {
    LogSystem entity = BeanUtil.copyProperties(req, LogSystem.class);
    logSystemMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(LogSystemAddDTO req) {
    LogSystem entity = BeanUtil.copyProperties(req, LogSystem.class);
    logSystemMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public LogSystemVO view(Long id) {
    LogSystem entity = logSystemMapper.selectById(id);
    return BeanUtil.copyProperties(entity, LogSystemVO.class);
  }
}
